/*
 * Copyright 2014 netfinworks.com, Inc. All rights reserved.
 */
package org.rency.mq.activemq.listener.factory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jms.listener.SessionAwareMessageListener;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

/**
 * <p></p>
 *
 * @author zhangjiewen
 * @version $Id: DefaultSessionAwareMessageListener.java, v 0.1 14-2-20 下午5:59 zhangjiewen Exp $
 */
public class DefaultSessionAwareMessageListener extends DefaultMessageListener implements SessionAwareMessageListener {
    private static Logger logger = LoggerFactory.getLogger(DefaultMessageListener.class);
    @Override
    public void onMessage(Message message, Session session) throws JMSException {
        try{
            super.onMessage(message);
        }catch (Throwable t){
            logger.error("Listenning message error. Message:{} ",message,t);
            if(session.getAcknowledgeMode() == Session.CLIENT_ACKNOWLEDGE){
                logger.info("session acknowledgeMode is client_acknowledge.recover session.");
                session.recover();
            }
        }
    }
}
